<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Admin\Reservation;
use App\Services\Admin\Store;
use Illuminate\Http\Request;
use App\Services\Admin\GroupClass;

class GroupClassController extends Controller
{
    public function index()
    {
        $result = [
            'result'=>GroupClass::lists(),
            'group_class_name'=>\request()->get('group_class_name'),
            'select_store'=>Store::where_lists(),
            'store_id'=>\request()->get('store_id'),
        ];

        return view('admin.group_class.index', $result);
    }

    public function camp_info($id)
    {

        $result = [
            'result'=>GroupClass::camp_info($id)->get()->toArray()
        ];

        return view('admin.group_class.camp_info', $result);
    }

    public function add()
    {
        $reservation_lists = Reservation::lists()->whereHas('quarterly',function ($query){
            $where = [];

            $where[] = ['start_time', '<=', date('Y-m-d')];
            $where[] = ['end_time', '>=', date('Y-m-d')];
            $where[] = ['is_display', '=', 1, 'OR'];

            $where[] = ['start_time', '>=', date('Y-m-d')];
            $where[] = ['is_display', '=', 1];

            $query->where($where);
        })->where('is_camp','1')->get()->toArray();

        foreach ($reservation_lists as $k => $time_v) {
            $reservation_lists[$k]['teacher'] = Reservation::class_bass_info_teacher_arr($time_v['class_base_info_teacher']);
            if(empty($reservation_lists[$k]['teacher']['id']))unset($reservation_lists[$k]);
        }

        $result = [
            'reservation_lists'=> $reservation_lists,
            'select_store'=>Store::where_lists(),
            'select_id' => '',
        ];

        return view('admin.group_class.edit',$result);
    }

    public function edit(Request $request, $id)
    {
        $result = GroupClass::info($id);

        request()->offsetSet('store_id',$result->store_id);

        $reservation_lists = Reservation::lists()->whereHas('quarterly',function ($query){
            $where = [];

            $where[] = ['start_time', '<=', date('Y-m-d')];
            $where[] = ['end_time', '>=', date('Y-m-d')];
            $where[] = ['is_display', '=', 1, 'OR'];

            $where[] = ['start_time', '>=', date('Y-m-d')];
            $where[] = ['is_display', '=', 1];

            $query->where($where);
        })->where('is_camp','1')->get()->toArray();

        foreach ($reservation_lists as $k => $time_v) {
            $reservation_lists[$k]['teacher'] = Reservation::class_bass_info_teacher_arr($time_v['class_base_info_teacher']);
            if(empty($reservation_lists[$k]['teacher']['id']))unset($reservation_lists[$k]);
        }

        $result = [
            'id'=>$id,
            'result'=> $result,
            'group_class_info'=>GroupClass::group_class_info($id)->pluck('class_base_info_id')->toArray(),
            'remedial_teaching_number'=>GroupClass::info($id)->remedial_teaching_number,
            'reservation_lists'=> $reservation_lists,
            'select_store'=>Store::where_lists(),
            'select_id' => $result->store_id,
        ];

        return view( 'admin.group_class.edit', $result);
    }

    public function do(Request $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $image_avatar = !empty($request->file('cove_img')) ? $request->file('cove_img') : [];

            if (!empty($image_avatar)) {

                $path = $image_avatar->store('images/'. date('Ymd'));

                $input['cove_img'] = "/upload/".$path;

            } else {

                unset($input['cove_img']);

            }

            if(empty($input['store_id'])) return self::fail('請填寫門店');
            if(empty($input['registration_start_time'])) return self::fail('請填寫開始報名時間');
            if(empty($input['registration_time'])) return self::fail('請填寫結束報名時間');

            $registration_start_time = strtotime("{$input['registration_start_time']}");
            $registration_time = strtotime("{$input['registration_time']}");

            if($registration_start_time > $registration_time) return self::fail('開始報名時間大於結束報名時間');

            if (empty($input['id'])) {

                unset($input['id']);

                $group_result = GroupClass::add($input);

            } else {

                $group_result = GroupClass::edit($input['id'], $input);

            }

            if(!$group_result['bool'])return self::fail($group_result['message']);

            return self::success([]);
        }
    }

    public function del($id)
    {

        GroupClass::del($id);

        return self::success([]);

    }

    public function reservation_lists()
    {
        $input = \request()->all();

        $reservation_lists = Reservation::lists()->whereHas('quarterly',function ($query){
            $where = [];

            $where[] = ['start_time', '<=', date('Y-m-d')];
            $where[] = ['end_time', '>=', date('Y-m-d')];
            $where[] = ['is_display', '=', 1, 'OR'];

            $where[] = ['start_time', '>=', date('Y-m-d')];
            $where[] = ['is_display', '=', 1];

            $query->where($where);
        })->where('is_camp','1')->get()->toArray();

        foreach ($reservation_lists as $k => $time_v) {
            $reservation_lists[$k]['teacher'] = Reservation::class_bass_info_teacher_arr($time_v['class_base_info_teacher']);
            if(empty($reservation_lists[$k]['teacher']['id']))unset($reservation_lists[$k]);
        }

        if(!empty($input['id'])) {
            $group_class_info = GroupClass::group_class_info($input['id'])->pluck('class_base_info_id')->toArray();
        }else{
            $group_class_info = [];
        }

        $result = [
            'group_class_info'=>$group_class_info,
            'reservation_lists'=>$reservation_lists
        ];

        return self::success($result);
    }
}
